<template>
  <div>
    <div class="docker">
      <div div="container">
        <!-- nav -->
        <div class="nav">
          <a v-if="$auth.front_username == ''">
            <a style="color:#DE4323;margin-left: 150px;">
              <router-link to="/login">亲，请登录</router-link>
            </a>
            <a class="div">免费注册</a>
          </a>
          <a v-else>
            <a style="color:#DE4323;margin-left: 150px;">
              <router-link to="/login">用户: {{ $auth.front_username }}</router-link>
            </a>
            <a class="div" @click="Logout">登出</a>
          </a>
          <a style="margin-left: 400px;"></a>
          <a class="push"></a>
          <a class="push">
            <router-link to="/">科购首页</router-link>
          </a>
          <a class="push">
            <router-link to="/GoodsCar"><img src="../assets/car.png">&nbsp购物车</router-link>
          </a>
          <a class="push">
            <router-link to="/orderList">订单详情</router-link>
          </a>
          <a class="push">
            <router-link to="/Recharge">充值页面</router-link>
          </a>
          <a class="push">
            <router-link to="/feedback">用户反馈</router-link>
          </a>
          <a class="push"></a>
        </div>
      </div>
      <div class="sign">
        <div class="log">
          <h1>科购</h1>
          <p>理想生活上科购</p>
        </div>
      </div>
<!--      <div class="search">-->
<!--        <input type="text">-->
<!--      </div>-->
<!--      <div class="button">-->
<!--        <input type="submit" value="搜索" class="bu">-->
<!--      </div>-->

    </div>
    <!--产品展示部分-->


    <!--内容展示-->
    <div class="content">
      <div class="jumbotron">
        <div class="img-2">

          <img :src="'data:image/jpeg;base64,'+ProductPic">
        </div>
        <div class="name2">
          <h5>{{ ProductName }}</h5>

        </div>
        <div class="Price">￥{{ ProductPrice }}</div>
        <div class="intro">
          <a>商品简介:</a>
          <p>{{ ProductDesc }} </p>
        </div>

        <div class="intro">
          <span style="font-weight: bolder">商品库存:</span>
          <span>{{ ProductNum }}</span>
        </div>

        <div class="addr" style="margin-top: 10px">
          <label style="font-size: 15px;">发货地址:</label>
          {{ ProductAddress }}
        </div>
        <div class="addr" style="margin-top: 10px">
          <label style="font-size: 15px;">收货地址:</label>
          <input type="text" placeholder="省-市-县-具体地址" v-model="NewAddress"></input>
        </div>
        <div class="purchasenumber" style="margin-top: 20px">
          <span style="font-weight: bolder;font-size: 15px;margin-top: 50px">购买数量：</span>
          <span><button @click="ReduceNum">-</button> </span>
          <span><input type="text" v-model="num" style="width: 50px"> </span>
          <span><button @click="AddNum">+</button> </span>
        </div>
        <div class="addpurchase">
          <button type="button" class="add mt25 ml68 btn-light" @click="AddGoodsCar">
            <i class="iconfont icon-gouwuchetianjia">&nbsp;&nbsp;加入购物车</i>
          </button>
          <button type="button" class="purchase mt25 ml18 btn-light" @click="BuyGoods">立即购买</button>
        </div>

      </div>
    </div>

    <div class="footer">
      <p>反馈建议 | 官方空间 | 空间活动 | 空间应用 | ABCmouse腾讯版 | 腾讯原创馆 | QQ互联 | QQ登录 | 社交组件 | 应用侵权投诉 | Complaint Guidelines</p>
      <p>Copyright 2005 - 2022 Tencent.All Rights Reserved.</p>
      <p><a href="https://beian.miit.gov.cn">湘ICP备2022018153号-1</a></p>
    </div>

  </div>
</template>

<script>
export default {
  name: "GoodsDetail",
  data() {
    return {
      ProductName: "",
      ProductDesc: '',
      ProductAddress: '',
      ProductNum: 0,
      ProductPic: '',
      ProductPrice: '',
      num: 1,
      NewAddress: '',//收货地址
    }
  },
  methods: {
    //商品购买
    BuyGoods() {
      if (this.NewAddress.length != 0 && this.num != 0 && this.ProductNum > this.num) { //信息不能缺失
        const form_data = new FormData;
        form_data.append("username", this.$auth.front_username)
        form_data.append("GoodsId", this.$route.params.id)
        form_data.append("GoodsNum", this.num),
          form_data.append("GoodsAddress", this.NewAddress)
        this.$http.post("/product/BuyGoods", form_data, {
          headers: {
            Authorization: "Bearer " + localStorage.getItem("front_token") //带着token去请求
          }
        }).then((rep) => {
          if (rep.data.code == 401) {
            this.$router.push("/login")
            this.$notify.info({
              title: '系统信息',
              message: rep.data.msg
            })
          } else {
            this.$notify.info({
              title: '系统信息',
              message: rep.data.msg
            })
          }
        }).catch((rep) => {
          console.log(rep)
        })
      } else {
        this.$notify.info({
          title: '系统信息',
          message: "购买信息有误"
        })
      }
    },
    //加入购物车
    AddGoodsCar() {
      if (this.NewAddress.length != 0 && this.num != 0 && this.ProductNum > this.num) {
        this.$http.get("/product/AddGoodsCar", {
          params: {
            username: this.$auth.front_username, //购买人的用户名
            GoodsId: this.$route.params.id,   //商品id
            GoodsNum: this.num, //购买的商品的数量
            Address: this.NewAddress   //收货地址
          },
          headers: {
            Authorization: "Bearer " + localStorage.getItem("front_token") //带着token去请求
          }
        }).then((rep) => {
          if (rep.data.code == 401) {
            this.$router.push("/login")
            this.$notify.info({
              title: '系统信息',
              message: rep.data.msg
            })
          } else {
            this.$notify.info({
              title: '系统信息',
              message: rep.data.msg
            })
          }
        }).catch((rep) => {
          console.log(rep)
        })
      } else {
        alert("购买信息有误")
      }
    },
    //商品数量+
    AddNum() {
      if (this.num < this.ProductNum) {
        this.num += 1 //实现自加
      }
    },
    //商品数量-
    ReduceNum() {
      if (this.num >= 2) {
        this.num -= 1 //实现自减
      }
    },
    Logout() {
      this.$auth.delFrontAuth()
      this.$router.push("/login")
    },
    //获取商品信息
    GetGoodsInfo() {
      //需要带着token去请求
      this.$http.get("/product/GoodsDetail", {
        params: {
          id: this.$route.params.id
        }, headers: {
          Authorization: "Bearer " + localStorage.getItem("front_token") //带着token去请求
        }
      }).then((rep) => {
        if (rep.data.code == 200) {
          this.ProductName = rep.data.ProductName
          this.ProductDesc = rep.data.ProductDesc
          this.ProductAddress = rep.data.ProductAddress
          this.ProductNum = rep.data.ProductNum
          this.ProductPic = rep.data.ProductPic
          this.ProductPrice = rep.data.ProductPrice
        } else if (rep.data.code == 401) {
          this.$router.push("/login")
          this.$notify.info({
            title: '系统信息',
            message: '请重新登陆'
          })
        }
      }).catch((rep) => {
        console.log(rep)
      })
    }
  },
  mounted() {
    this.GetGoodsInfo();
  }
}
</script>

<style scoped>
.nav {
  border: 1px solid #F5F5F5;
  width: 100%;
  font-size: 10px;
  background-color: #F5F5F5;
}

.nav a {
  margin-top: 5px;
  margin-bottom: 5px;
  cursor: pointer;
}

.nav .div {
  margin-left: 30px;
  color: #8D8D8D;
}

.nav .push {
  margin-left: 30px;
  color: #8D8D8D;
}

.nav .push img {
  width: 15px;
  margin-left: 10px;
}

.logo {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  margin-left: 200px;
  margin-top: 40px;
  color: #EB5E2A;
}

.logo p {
  font-size: 20px;
  margin-top: -10px;
  margin-left: 9px;
}

.logo h5 {
  color: #171A1D;
  margin-top: -80px;
  margin-left: 85px;
}

.sign {
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  margin-left: 130px;
  margin-top: 8px;
  color: #EB5E2A
}

.log h1 {
  font-size: 55px;
}

.log p {
  font-size: 3px;
  letter-spacing: 2px;
  margin-top: -10px;
  margin-left: 5px;
  letter-spacing: 3px;
}

.search input {
  border: 3px solid #EB5E2A;
  width: 300px;
  height: 45px;
  border-radius: 20px;
}

.search {
  margin-top: -90px;
  margin-left: 930px;
}

.button .bu {

  border: 1px solid #EB5E2A;
  width: 70px;
  background-color: #EB5E2A;
  color: #fff;
  font-weight: bolder;
  height: 39.5px;
  border-radius: 20px;
}

.button {
  margin-top: -42.5px;
  margin-left: 1159px;
}

.but input {
  border: 1px solid #EB5E2A;
  width: 70px;

  color: #EB5E2A;
  font-weight: bolder;
  height: 39.5px;
  border-radius: 20px;
}

.but {
  margin-top: 1px;
  margin-left: 80px;
}

.jumbotron {
  height: 450px;
  width: 83.2%;
  margin-left: 130px;
  margin-top: 80px;
}

.img-2 img {
  width: 320px;
  height: 400px;
  margin-top: -20px;
}

.name2 h5 {
  margin-top: -405px;
  margin-left: 400px;
  letter-spacing: 2px;
}

.name2 p {
  margin-top: -10px;
  margin-left: 400px;
  color: #DE4323;
  font-size: 10px;
}

.Price {
  margin-top: -10px;
  margin-left: 390px;
  color: #EB5E2A;
  font-size: 40px;

}

.distribution {
  margin-left: 400px;
}

.distribution h5 {
  margin-left: 50px;
  margin-top: -20px;
  font-size: 15px;
  letter-spacing: 2px;
}

.addr {
  margin-left: 400px;
}

.addr input {
  width: 300px;
}

.intro {
  margin-left: 400px;
}

.intro a {
  color: #000000;
}

.intro p {
  color: #383D41;
}

.purchasenumber {
  margin-left: 400px;
}

.addpurchase > button {
  width: 186px;
  height: 45px;
  font: bold 16px/45px "微软雅黑";
  cursor: pointer;
}

.addpurchase .add {
  color: #fafbf3;
  background-color: #f26a12;
  border: 1px solid #f26a12;
}

.addpurchase .purchase {
  border: 1px solid #FEF9F6;
  background-color: #FEF9F6;
  color: #F46C15;
}

.addpurchase {
  margin-left: 400px;
  margin-top: 27px;

}

.footer {
  margin-top: 20px;
  border: 1px solid #fff;
  text-align: center;
  font-size: 10px;
  background-color: #fff;
  padding-top: 10px;
  padding-bottom: 10px;
}

.footer p {
  margin-bottom: 0px;
}
</style>
